package com.cxs.ocrrecognition.slice;

import com.cxs.ocrrecognition.ResourceTable;
import com.cxs.ocrrecognition.utils.ImageUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.ai.cv.common.*;
import ohos.ai.cv.text.ITextDetector;
import ohos.ai.cv.text.Text;
import ohos.ai.cv.text.TextConfiguration;
import ohos.ai.cv.text.TextDetectType;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.media.image.PixelMap;

public class MainAbilitySlice extends AbilitySlice {
    static final HiLogLabel LABEL = new HiLogLabel(HiLog.LOG_APP, 0x00101, "MainAbility");
    private boolean isConnect = false;
    private ohos.agp.components.Text textInfo;
    ITextDetector textDetector;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        textInfo = (ohos.agp.components.Text)findComponentById(ResourceTable.Id_text_info);

        // 建立引擎连接
        int init = VisionManager.init(MainAbilitySlice.this, connectionCallback);
        // 实例化文本识别接口
        textDetector = VisionManager.getTextDetector(MainAbilitySlice.this);

        PixelMap pixelMap = ImageUtil.getPixelMap(this, ResourceTable.Media_test);
        Image image = (Image)findComponentById(ResourceTable.Id_image);
        image.setPixelMap(pixelMap);
        // 传入待检测图片
        VisionImage visionImage = VisionImage.fromPixelMap(pixelMap);

        TextConfiguration.Builder builder = new TextConfiguration.Builder();
        builder.setProcessMode(VisionConfiguration.MODE_IN);
        builder.setDetectType(TextDetectType.TYPE_TEXT_DETECT_FOCUS_SHOOT);
        builder.setLanguage(TextConfiguration.CHINESE);
        TextConfiguration textConfiguration = builder.build();
        textDetector.setVisionConfiguration(textConfiguration);

        findComponentById(ResourceTable.Id_btn_recognition).setClickedListener(c->{
            showInfo("isConnect：" + isConnect);
            if (isConnect) {
                int result = textDetector.detect(visionImage, null, callback); // 异步
                showInfo("result: " + result);
            }
        });
    }
    VisionCallback<Text> callback = new VisionCallback<Text>() {
        @Override
        public void onResult(Text text) {
            showInfo("onResult: " + text.getValue());
        }

        @Override
        public void onError(int i) {
            showInfo("onError: " + i);
        }

        @Override
        public void onProcessing(float v) {
            showInfo("onProcessing: " + v);
        }
    };

    ConnectionCallback connectionCallback = new ConnectionCallback() {
        @Override
        public void onServiceConnect() {
            isConnect = true;
        }

        @Override
        public void onServiceDisconnect() {
            isConnect = false;
        }
    };

    private void showInfo(String s) {
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                textInfo.append("\n" + s);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
